package com.neusoft.yuejian12.yuejian_12.fragment;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.adapter.ClubListAdapter;

import com.neusoft.yuejian12.yuejian_12.javabean.ClubBean;
import com.neusoft.yuejian12.yuejian_12.model.ClubModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/26.
 */

public class ClubFragment extends Fragment {
    private RecyclerView recyclerView;
    private View view = null;
    private ImageView c_pic;
    private TextView c_name;
   // private RollPagerView rollPV = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab02, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_club);
       // recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));//设置布局
       // recyclerView.setLayoutManager(new GridLayoutManager(this.getActivity(),2));//表格布局
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        recyclerView.addItemDecoration(decoration);
        initView();
        getData();
        //initEvents();
        return view;
    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpacesItemDecoration(int space) {
            this.space=space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    private void initView()
    {
        c_name= (TextView) view.findViewById(R.id.c_name);
       c_pic= (ImageView) view.findViewById(R.id.c_pic);
       // rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
    }

    //轮播图
//    public void initEvents(){
//        rollPV.setPlayDelay(3000);
//        //设置图片切换动画时间
//        rollPV.setAnimationDurtion(500);
//        //设置指示器:
//        //rollPV.setHintView(new IconHintView());
//        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
//        rollPV.setHintView(new ColorPointHintView(getActivity(),
//                getResources().getColor(R.color.colorPrimary),
//                Color.WHITE));
//        //设置适配器
//        rollPV.setAdapter(new RollPagerAdapter());
//
//        //设置每一个图片的点击事件
//        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(int position) {
//                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();
//
//            }
//        });*/
//
//
//    }
//    private int images[] = {
//            R.drawable.cluba,
//            R.drawable.clubb,
//            R.drawable.clubd
//    };
//
//    //设置适配器
//    private class RollPagerAdapter extends StaticPagerAdapter {
//
//        @Override
//        public View getView(ViewGroup container, int position) {
//
//            ImageView view=new ImageView(container.getContext());
//            //设置图片资源
//            view.setImageResource(images[position]);
//            //设置高度和宽度
//            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                    ViewGroup.LayoutParams.MATCH_PARENT));
//            //设置拉伸方式
//            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            return view;
//        }
//
//        @Override
//        public int getCount() {
//            return images.length;
//        }
//    }

    //获取列表数据
    private void getData() {
        ClubModel clubModel=new ClubModel();
        Call<List<ClubBean>> call=clubModel.getClublist();
        System.out.print("c_name");
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                List<ClubBean> data = response.body();  //获取数据
           //     System.out.printf("data");
                recyclerView.setAdapter(new ClubListAdapter(ClubFragment.this.getActivity(), data));
            }
            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {

            }
        });
    }


}